using UnityEngine;
using UnityEngine.Rendering;


public partial class IBL
{
    const string bufferName = "IBL";

    const string iBLKeyword = "_IBL_RP_ON";

    public static readonly int diffuseHDR = Shader.PropertyToID("_iBLDiffuseHDR");
    public static readonly int diffuseScale = Shader.PropertyToID("_iBLDiffuseScale");
    public static readonly int specularHDR = Shader.PropertyToID("_iBLSpecularHDR");
    public static readonly int specularScale = Shader.PropertyToID("_specularScale");
    public static readonly int iBLBrdfLut = Shader.PropertyToID("_iBLBrdfLut");

    CommandBuffer buffer = new CommandBuffer
    {
        name = bufferName
    };

    ScriptableRenderContext context;

    IBLSettings settings;

    Camera camera;
    
    public void Setup(ScriptableRenderContext context, Camera camera, IBLSettings settings)
    {
        this.context = context;
        this.camera = camera;
        this.settings = settings;

        buffer.SetGlobalTexture(diffuseHDR, settings.diffuseHDR);
        buffer.SetGlobalFloat(diffuseScale, settings.diffuseScale);
        buffer.SetGlobalTexture(specularHDR, settings.specularHDR);
        buffer.SetGlobalFloat(specularScale, settings.specularScale);
        buffer.SetGlobalTexture(iBLBrdfLut, settings.brdfLut);
        buffer.EnableShaderKeyword(iBLKeyword);
        Submit();
    }

    public void UnSetup(ScriptableRenderContext context)
    {
        this.context = context;
        buffer.DisableShaderKeyword(iBLKeyword);
        Submit();
    }


    void Submit()
    {
        ExecuteBuffer();
        context.Submit();
    }

    void ExecuteBuffer()
    {
        context.ExecuteCommandBuffer(buffer);
        buffer.Clear();
    }

    public static bool IsActive(bool isRpOpen, IBLSettings rpSetting, bool isCameraOpen, IBLSettings cameraSetting)
    {
        return true;
    }

}
